package com.wt.admin.config;

import com.wt.admin.config.cache.Cache;
import com.wt.admin.config.cache.CacheManager;
import com.wt.admin.domain.vo.sys.UserVO;
import lombok.Data;
import org.dromara.easyai.entity.KeyWordForSentence;
import org.dromara.easyai.naturalLanguage.TalkToTalk;
import org.dromara.easyai.naturalLanguage.languageCreator.CatchKeyWord;
import org.dromara.easyai.naturalLanguage.word.MyKeyWord;
import org.dromara.easyai.naturalLanguage.word.WordEmbedding;
import org.dromara.easyai.rnnJumpNerveCenter.RRNerveManager;
import org.dromara.easyai.yolo.FastYolo;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

@Configuration
public class GlobalBeanConfig {

    /**
     * 系统国际化文件配置
     * @return MessageSource
     */
    @Bean("messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        // msg 是文件前缀
        messageSource.setBasename("classpath:i18n/msg");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public Cache<Long, UserVO> userCache(){
        return CacheManager.getCache("user");
    }

    @Bean("newAsyncExecutor")
    public Executor newAsync() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        taskExecutor.setCorePoolSize(50);
        // 线程池维护线程的最大数量，只有在缓冲队列满了以后才会申请超过核心线程数的线程
        taskExecutor.setMaxPoolSize(100);
        //缓存队列
        taskExecutor.setQueueCapacity(50);
        //允许的空闲时间，当超过了核心线程数之外的线程在空闲时间到达之后会被销毁
        taskExecutor.setKeepAliveSeconds(100);
        //异步方法内部线程名称
        taskExecutor.setThreadNamePrefix("定时任务-");
        //拒绝策略
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Bean("publicThread")
    public Executor publicThread() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        taskExecutor.setCorePoolSize(50);
        // 线程池维护线程的最大数量，只有在缓冲队列满了以后才会申请超过核心线程数的线程
        taskExecutor.setMaxPoolSize(100);
        //缓存队列
        taskExecutor.setQueueCapacity(50);
        //允许的空闲时间，当超过了核心线程数之外的线程在空闲时间到达之后会被销毁
        taskExecutor.setKeepAliveSeconds(100);
        //异步方法内部线程名称
        taskExecutor.setThreadNamePrefix("通用任务-");
        //拒绝策略
        taskExecutor.initialize();
        return taskExecutor;
    }

    /**
     * 词向量嵌入器,语义神经
     * @return
     */
    @Bean("wordEmbedding")
    public Cache<String,WordAndRRManager> wordEmbedding(){
        return CacheManager.getCache("wordEmbedding");
    }

    @Bean("myKeyWord")
    public Cache<Integer,MyKeyWord> myKeyWordMap(){
        return CacheManager.getCache("myKeyWord");
    }

    @Bean("catchKeyWord")
    public Cache<Integer, CatchKeyWord> catchKeyWordMap(){
        return CacheManager.getCache("catchKeyWord");
    }

    @Bean("sensorKeyWordMapper")
    public Cache<Integer,Cache<Integer,List<KeyWordForSentence>>> sensorKeyWordMapper(){
        return CacheManager.getCache("sensorKeyWordMapper");
    }

    @Bean("imageYoloManager>")
    public Cache<Integer,FastYolo> imageYoloManager(){
        return CacheManager.getCache("imageYoloManager");
    }

    @Bean("keyWordValueList")
    public List<KeywordValue> keyWordValueList(){
        return new ArrayList<>();
    }


    @Data
    public static class KeywordValue{
        private String keywordValue;
        private Integer typeId;
        private Long index;//该关键词索引id
        public KeywordValue(String keywordValue, Integer typeId, Long index) {
            this.keywordValue = keywordValue;
            this.typeId = typeId;
            this.index = index;
        }
        public KeywordValue(String keywordValue, Integer typeId) {
            this.keywordValue = keywordValue;
            this.typeId = typeId;
        }
    }

    @Data
    public static class WordAndRRManager {

        private WordEmbedding wordEmbedding;
        private RRNerveManager rrNerveManager;
        private TalkToTalk talkToTalk;

        public WordAndRRManager() {
            this.wordEmbedding = new WordEmbedding();
            this.rrNerveManager = new RRNerveManager(wordEmbedding);
        }

    }


}
